import {request} from "../../utils/util";

const app = getApp()
Page({
  data: {
    top: app.globalData.rightBarInfo.top,
    height: app.globalData.rightBarInfo.height,
    userInfo: {},
    isLogin: false,
    messageCount: 0
  },
  onShow () {
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo
      })
      if (this.data.userInfo.access_token) {
        this.getMessageCount()
      }
    }
  },
  getUserInfo (e) {
    let userInfo = e.detail.userInfo
    this.getCode((code) => {
      wx.showLoading({
        title: '加载中..',
        mask: true
      })
      request({
        url: `/api/web/login`,
        method: 'POST',
        data: {
          code,
          nickname: userInfo.nickName,
          avatar: userInfo.avatarUrl
        },
        success: ({ code, data, message }) => {
          wx.hideLoading()
          if (code === 200) {
            this.setData({
              userInfo: data
            })
            app.globalData.userInfo = data
            if (this.data.userInfo.access_token) {
              this.getMessageCount()
            }
            if (!data.telephone) {
              this.bindPhone()
            } else {
              wx.showToast({
                title: '登录成功！',
                icon: 'success',
                duration: 2000
              })
            }
            wx.setStorage({
              key: "userInfo",
              data: this.data.userInfo
            })
          } else {
            app.globalData.userInfo = {}
            this.setData({
              userInfo: {}
            })
            wx.showToast({
              title: message || '登录失败！',
              icon: 'none',
              duration: 2000
            })
          }
        },
        fail: err => {
          app.globalData.userInfo = {}
          this.setData({
            userInfo: {}
          })
          wx.showToast({
            title: '登录失败！',
            icon: 'none',
            duration: 2000
          })
        }
      })
    })
  },
  getCode(callback) {
    wx.login({
      timeout: 10000,
      success: function (re) {
        if(re.code){
          callback(re.code)
        }
      },
      fail: (err) => {
        wx.showModal({
          title: '提示',
          content: '微信登录返回信息错误！可选择重试，如果多次出现该错误，建议您稍后再试。',
          confirmText: '重试',
          success: (res) => {
            if (res.confirm) {
              this.getCode(callback)
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
      }
    })
  },
  bindPhone() {
    wx.navigateTo({
      url: '/pages/login/index'
    })
  },
  toSeeUserInfo () {
    if (!this.data.userInfo.nickname) {
      return false
    } else if (!this.data.userInfo.telephone) {
      wx.showToast({
        title: '请绑定手机号',
        icon: 'none',
        duration: 2000
      })
      setTimeout(() => {
        wx.navigateTo({
          url: '/pages/login/index'
        })
      }, 1000)
      return false
    }
    wx.navigateTo({
      url: '/pages/personalInfo/index'
    })
  },
  checkLogin() {
    if (!this.data.userInfo.nickname) {
      wx.showToast({
        title: '请先登录',
        icon: 'none',
        duration: 2000
      })
      return false
    } else if (!this.data.userInfo.telephone) {
      wx.showToast({
        title: '请绑定手机号',
        icon: 'none',
        duration: 2000
      })
      setTimeout(() => {
        wx.navigateTo({
          url: '/pages/login/index'
        })
      }, 1000)
      return false
    }
    return true
  },
  toSportOrder () {
    if (!this.checkLogin()) {
      return false
    }
    wx.navigateTo({
      url: '/pages/sportsOrder/index'
    })
  },
  toGoodsOrder () {
    if (!this.checkLogin()) {
      return false
    }
    wx.navigateTo({
      url: '/pages/goodsOrder/index'
    })
  },
  toMessage () {
    if (!this.checkLogin()) {
      return false
    }
    wx.navigateTo({
      url: '/pages/message/index'
    })
  },
  toIntegral () {
    if (!this.checkLogin()) {
      return false
    }
    wx.navigateTo({
      url: '/pages/integral/index'
    })
  },
  toBalance () {
    if (!this.checkLogin()) {
      return false
    }
    wx.navigateTo({
      url: '/pages/balance/index'
    })
  },
  toConsumptionRecords () {
    if (!this.checkLogin()) {
      return false
    }
    wx.navigateTo({
      url: '/pages/consumptionRecords/index'
    })
  },
  toFeedback () {
    wx.navigateTo({
      url: '/pages/feedback/index'
    })
  },
  getMessageCount() {
    request({
      url: `/api/web/personal/message_num`,
      success: ({ code, data }) => {
        wx.hideLoading()
        if (code === 200) {
          this.setData({
            messageCount: data.message_count
          })
        }
      }
    })
  }
})
